package com.gm.commonlib.module.print;

import android.content.Context;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.webkit.WebView;

import com.gm.commonlib.interfaces.IResultListener;
import com.gm.commonlib.util.logUtils.LogUtil;

public class PrintUtil {
    public static final String jobName="print"+System.currentTimeMillis();
    public synchronized static void createWebPrintJob(Context context, WebView webView, IResultListener<Boolean> iResultListener) {

        try {
            PrintManager printManager = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
            }
            PrintDocumentAdapter printAdapter = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                printAdapter = webView.createPrintDocumentAdapter(jobName);
            }
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                printManager.print(jobName, printAdapter,
                        new PrintAttributes.Builder().build());
                iResultListener.onResult(true);

            }

        } catch (Exception e) {
            iResultListener.onResult(false);
        }

    }

}
